package com.jz.kufangkuqu.shuxing.service.impl;

import com.jz.beijianbao.beijian.personEnum.PersonEnum;
import com.jz.kufangkuqu.shuxing.bean.*;
import com.jz.kufangkuqu.shuxing.mapper.ShuXingMapper;
import com.jz.kufangkuqu.shuxing.service.ShuXingService;
import com.jz.util.AllRecordsVo;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ShuXingServiceImpl implements ShuXingService {

    @Autowired
    private ShuXingMapper shuXingMapper;



    @Override
    public ResultVO queryPage(ShuXingSelect shuXingSelectVo) {
        List<ShuXing> shuXingList = shuXingMapper.queryPage(shuXingSelectVo);
        for (ShuXing plant: shuXingList) {
            Integer enabled = plant.getStatus() ? 1:0;
            plant.setStatusName(PersonEnum.getName(enabled));
        }
        int count = shuXingMapper.count(shuXingSelectVo);
        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setPageIndex(shuXingSelectVo.getPageIndex());
        allRecordsVo.setPageSize(shuXingSelectVo.getPageSize());
        allRecordsVo.setTotalSize(count);
        allRecordsVo.countPage(count);
        allRecordsVo.setDataList(shuXingList);
        return ResultBuildVO.success(allRecordsVo);
    }


    @Override
    public ResultVO add(ShuXingAdd shuXingAdd) {
        Date now = new Date();
        ShuXing legalPlantVo1 = new ShuXing();
        BeanUtils.copyProperties(shuXingAdd,legalPlantVo1);
        legalPlantVo1.setCreateName("从session中获取");
        legalPlantVo1.setCreateTime(now);
        shuXingMapper.add(legalPlantVo1);
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO update(ShuXingUpdate shuXingUpdate) {
        Date now = new Date();
        ShuXing legalPlant1 = new ShuXing();
        BeanUtils.copyProperties(shuXingUpdate,legalPlant1);
        legalPlant1.setUpdateName("从session获取");
        legalPlant1.setUpdateTime(now);
        int version = shuXingMapper.updatePlant(legalPlant1);
        if (version==0){
            return ResultBuildVO.error("情刷新后修改");
        }
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO updateStatus(ShuXingUpdatestatus shuXingUpdatestatus) {
        Date now = new Date();
        ShuXing legalPlant1 = new ShuXing();
        BeanUtils.copyProperties(shuXingUpdatestatus,legalPlant1);
        legalPlant1.setUpdateName("从session获取");
        legalPlant1.setUpdateTime(now);
        shuXingMapper.updateStatus(legalPlant1);
        return ResultBuildVO.success();
    }


}
